package com.zzyl.nursing.service.impl;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;

import cn.hutool.core.collection.CollUtil;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.nursing.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingProjectMapper;
import com.zzyl.nursing.domain.NursingProject;
import com.zzyl.nursing.service.INursingProjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 护理项目Service业务层处理
 *
 * @author alexis
 * @date 2024-12-30
 */
@Service
public class NursingProjectServiceImpl extends ServiceImpl<NursingProjectMapper, NursingProject> implements INursingProjectService {
    @Autowired
    private NursingProjectMapper nursingProjectMapper;
    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;

    //redis里的key,抽取为常量
    private static final String NURSING_PROJECT_KEY = "NursingProject:all";

    /**
     * 查询护理项目
     *
     * @param id 护理项目主键
     * @return 护理项目
     */
    @Override
    public NursingProject selectNursingProjectById(Long id) {
        return nursingProjectMapper.selectById(id);
    }

    /**
     * 查询护理项目列表
     *
     * @param nursingProject 护理项目
     * @return 护理项目
     */
    @Override
    public List<NursingProject> selectNursingProjectList(NursingProject nursingProject) {
        //从缓存中获取
        List<NursingProject> nursingProjects = (List<NursingProject>) redisTemplate.opsForValue().get(NURSING_PROJECT_KEY);
        // 缓存中有数据，直接返回
        if (CollUtil.isNotEmpty(nursingProjects)) {
            return nursingProjects;
        }
        //缓存中没有数据，从数据库查找

        nursingProjects = nursingProjectMapper.selectNursingProjectList(nursingProject);

        //将数据写入缓存
        redisTemplate.opsForValue().set(NURSING_PROJECT_KEY, nursingProjects, 12, TimeUnit.HOURS);


        return nursingProjects;
    }

    /**
     * 新增护理项目
     *
     * @param nursingProject 护理项目
     * @return 结果
     */
    @Override
    public int insertNursingProject(NursingProject nursingProject) {
        int insert = nursingProjectMapper.insert(nursingProject);
        deleteCache();
        return insert;
    }


    private void deleteCache() {
        //删除缓存
        redisTemplate.delete(NURSING_PROJECT_KEY);
    }

    /**
     * 修改护理项目
     *
     * @param nursingProject 护理项目
     * @return 结果
     */
    @Override
    public int updateNursingProject(NursingProject nursingProject) {
        int updateById = nursingProjectMapper.updateById(nursingProject);
        deleteCache();
        return updateById;
    }

    /**
     * 批量删除护理项目
     *
     * @param ids 需要删除的护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingProjectByIds(Long[] ids) {
        int deleteBatchIds = nursingProjectMapper.deleteBatchIds(Arrays.asList(ids));
        deleteCache();
        return deleteBatchIds;
    }

    /**
     * 删除护理项目信息
     *
     * @param id 护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingProjectById(Long id) {
        int deleteById = nursingProjectMapper.deleteById(id);
        deleteCache();
        return deleteById;
    }

    /**
     * 查询所有护理项目VO列表
     *
     * @return 结果
     */
    @Override
    public List<NursingProjectVo> getAllProjects() {
        return nursingProjectMapper.getAllProjects();
    }
}
